﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace CloudTaobao
{
    public partial class ItemPage : PhoneApplicationPage
    {
        Item item;
        Find fd = new Find();
        //bool needRefreshDetial = true;
        bool needRefreshComment = true;
        bool needRefreshBrief = true;

        public ItemPage()
        {
            InitializeComponent();

            fd.getItemBriefFinishedEvent += new GetItemBriefFinishedEventHandler(fd_getItemBriefFinishedEvent);
            fd.getItemDetialFinishedEvent += new GetItemDetialFinishedEventHandler(fd_getItemDetialFinishedEvent);
            fd.startLoadingEvent += new StartLoadingEventHandler(fd_startLoadingEvent);
            fd.endLoadingEvent += new EndLoadingEventHandler(fd_endLoadingEvent);
            fd.tradeRateList.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(tradeRateList_CollectionChanged);

            TradeRateList.ItemsSource = fd.tradeRateList;
        }

        void tradeRateList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (TradeRateList.Items.Count == 0 || TradeRateList.Items.Count == 1)
                return;
            TradeRateList.ScrollIntoView(TradeRateList.Items[0]);
            TradeRateList.UpdateLayout();
        }

        void fd_getItemDetialFinishedEvent(object source, GetItemFinishedEventArgs args)
        {
            item.wap_desc = args.item.wap_desc;

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                //DetialGrid.DataContext = item;
            });
        }

        void fd_getItemBriefFinishedEvent(object source, GetItemFinishedEventArgs args)
        {
            item.ems_fee = args.item.ems_fee;
            item.express_fee = args.item.express_fee;
            item.freight_payer = args.item.freight_payer;
            item.location = args.item.location;
            item.num = args.item.num;
            if (item.pic_url == "")
                item.pic_url = args.item.pic_url;
            item.stuff_status = args.item.stuff_status;
            item.type = args.item.type;
            item.wap_detail_url = args.item.wap_detail_url;
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                BriefGrid.DataContext = item;
            });

            needRefreshBrief = false;
        }

        void fd_endLoadingEvent()
        {
            loading.IsIndeterminate = false;
            loading.Visibility = Visibility.Collapsed;
        }

        void fd_startLoadingEvent(object source, StartLoadingEventArgs args)
        {
            loading.IsIndeterminate = true;
            loading.Visibility = Visibility.Visible;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            App app = Application.Current as App;
            item = app.tmpItem;
        }

        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (pivot.SelectedIndex) 
            {
                case 0:
                    {
                        ApplicationBar = null;
                        if (item == null || item.num_iid == "" || item.nick == "" || !needRefreshBrief)
                        {
                            return;
                        }
                        try
                        {
                            if (item.pic_url == "")
                                fd.GetItem(item.num_iid, item.nick, true);
                            else
                                fd.GetItem(item.num_iid, item.nick, true, false);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    break;
                /*case 1:
                    ApplicationBar = null;
                    if (item == null || !needRefreshDetial || item.num_iid == "" || item.nick == "")
                    {
                        return;
                    }
                    try
                    {
                        fd.GetItem(item.num_iid, item.nick, false);
                        needRefreshDetial = false;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    break;*/
                case 1:
                    {
                        ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar)Resources["commentBar"];
                        if (item == null||!needRefreshComment)
                        {
                            return;
                        }
                        try
                        {
                            if (item.num_iid != "" && item.nick != "")
                            {
                                fd.totalTradeRate = 0;
                                fd.currentTradeRatePage = 1;
                                fd.GetTradeRateList(item.nick, item.num_iid, fd.currentTradeRatePage, 20);
                                needRefreshComment = false;
                            }
                            else
                            {
                                MessageBox.Show("商品不存在");
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    break;
            }
        }

        private void previousCommentPage_Click(object sender, EventArgs e)
        {
            try
            {
                if (fd.currentTradeRatePage > 1 && item.nick != "" && item.num_iid != "")
                {
                    fd.GetTradeRateList(item.nick, item.num_iid, --fd.currentTradeRatePage, 20);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void refreshComments_Click(object sender, EventArgs e)
        {
            fd.totalTradeRate = 0;
            fd.currentTradeRatePage = 1;
            fd.GetTradeRateList(item.nick, item.num_iid, fd.currentTradeRatePage, 20);
        }

        private void nextCommentPage_Click(object sender, EventArgs e)
        {
            try
            {
                if ((fd.currentTradeRatePage * 20 < fd.totalTradeRate) && item.nick != "" && item.num_iid != "")
                {
                    fd.GetTradeRateList(item.nick, item.num_iid, ++fd.currentTradeRatePage, 20);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (item==null || item.num_iid == "" || item.nick == "")
                return;
            try
            {
                if (item.pic_url == "")
                    fd.GetItem(item.num_iid, item.nick, true);
                else
                    fd.GetItem(item.num_iid, item.nick, true, false);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Buy_Click(object sender, RoutedEventArgs e)
        {
            if (item.wap_detail_url == "")
                return;
            UriBuilder uri = new UriBuilder();
            uri.Query = "targetUrl=" + item.wap_detail_url;
            NavigationService.Navigate(new Uri("/WebBrowserPage.xaml" + uri.Query, UriKind.Relative));
        }

        private void chat_Click(object sender, RoutedEventArgs e)
        {
            if (item.nick == "")
                return;
            UriBuilder uri = new UriBuilder();
            uri.Query = "uid=" + item.nick + "&itemId=" + item.num_iid;
            NavigationService.Navigate(new Uri("/MessagePage.xaml" + uri.Query, UriKind.Relative));
        }
    }
}